const { TomatoUrl } = require("../../../config/config")

const back = wx.getBackgroundAudioManager();

const date = new Date()

const times = []

for (let i = 1; i <= 60; i++) {
  if(i<10){
    times.push('0'+i)
  }else{
    times.push(i)
  }
}
Page({
  data: {
    times:times,
    day: 25,//time
    time:'25',
    mTime:'1500000',
    timeStr:'25:00',
    pickerValue:[24],//设置picker的默认值为25，防止第一个数拖动时取不到
    clockShow:false,
    clockHeight:0,
    rate:'',
    timer:null,
    pickerShow:false,
    cancleShow:false,
    startShow:true,
    timeLong:25,
    tomatoRecord:{
      uid:'',
      timeLong:'',
    },
    sound:false
  },
  onLoad:function(){

    var res =  wx.getSystemInfoSync();
    var rate = 750 / res.windowWidth;
    this.setData({
      rate : rate,
      clockHeight : rate * res.windowHeight
    })
  },
  bindChange: function (e) {
    const val = e.detail.value
    this.setData({
      time: this.data.times[val[0]],
    })
    timeStr:  this.data.day+':00'
    if(this.data.time < 10){
      let oneDigitTime = this.data.time.substr(1,1)
      this.setData({
        timeLong : oneDigitTime
      })
    }else{
      this.setData({
        timeLong : this.data.time
      })
    }

  },
  picker:function(){
    this.setData({
      pickerShow:true
    })
  },
  timer:function(e){
    let _this = this
    this.setData({
      pickerShow:false,
      timeStr:  _this.data.time+':00',

    })
  },
  start:function(){
    this.setData({
      clockShow :　true,
      mTime : this.data.time*60*1000,
      startShow: false
    })
    this.drawBg()
    this.drawActive()

  },
  drawBg:function () {
    var lineWidth = 10 / this.data.rate; // px
    var ctx =wx.createCanvasContext('progress_bg');
    ctx.setLineWidth(lineWidth);
    ctx.setStrokeStyle('#F3F4F7');
    ctx.setLineCap('round');
    ctx.beginPath();
    ctx.arc(500/this.data.rate/2,500/this.data.rate/2,500/this.data.rate/2- 2* lineWidth,0,2*Math.PI,false);
    ctx.stroke();
    ctx.draw();
  },
  drawActive: function () {
    var _this = this;
    var timer = setInterval(function (){
      // 1.5 3.5
      // 0 2
      // 300000 100
      // 3000
      // 2 / 3000
      var angle = 1.5 + 2*(_this.data.time*60*1000 - _this.data.mTime)/(_this.data.time*60*1000);
      var currentTime = _this.data.mTime - 100
      _this.setData({
          mTime: currentTime
      });
      if(angle < 3.5) {
        if(currentTime % 1000 == 0) {
          var timeStr1 = currentTime / 1000; // s
          var timeStr2 = parseInt(timeStr1 / 60) // m
          var timeStr3 = (timeStr1 - timeStr2 * 60) >= 10 ? (timeStr1 - timeStr2 * 60) : '0' + (timeStr1 - timeStr2 * 60);
          var timeStr2 = timeStr2 >= 10 ? timeStr2:'0'+timeStr2;
          _this.setData({
            timeStr:timeStr2+':'+timeStr3
          })
        }
        var lineWidth = 10 / _this.data.rate; // px
        var ctx = wx.createCanvasContext('progress_active');
        ctx.setLineWidth(lineWidth);
        ctx.setStrokeStyle('#4B6FDE');
        ctx.setLineCap('round');
        ctx.beginPath();
        ctx.arc(500 / _this.data.rate / 2, 500 / _this.data.rate / 2, 500 / _this.data.rate / 2 - 2 * lineWidth, 1.5 * Math.PI, angle * Math.PI, false);
        ctx.stroke();
        ctx.draw();
      } else {
        wx.pauseBackgroundAudio({
          success: function(res){
            console.log('背景音乐暂停')
          }
        })

        _this.setData({
           'tomatoRecord.uid':wx.getStorageSync('userinfo').id,
           'tomatoRecord.timeLong' : _this.data.timeLong
        })
        wx.request({
          url: TomatoUrl,
          data: _this.data.tomatoRecord,
          method: 'POST',
        })
        _this.setData({
          timeStr:'00:00',
          startShow:true,
          clockShow : false
        })
        clearInterval(timer);
      }
    },100);
    _this.setData({
      timer:timer
    })
  },
  cancle:function(){
    wx.pauseBackgroundAudio({
      success: function(res){
        console.log('背景音乐暂停')
      }
    })
    this.setData({
      clockShow : false,
      startShow: true
    })
    clearInterval(this.data.timer)
  },
  toTomatoData:function(){
    wx.navigateTo({
      url: '/pages/tomato/tomatoRecord/tomatoRecord',
    })
  },
  selectBgMusic:function(){
    let _this = this
    wx.showActionSheet({
      itemList: ['静音','森林', '夏夜','春雨','海浪'],
      success (res) {
        if( res.tapIndex === 0){
          _this.setData({
            sound : false
          })
          wx.pauseBackgroundAudio({
            success: function(res){
              console.log('背景音乐暂停')
            }
          })
        }else{
          _this.setData({
            sound : true
          })
          _this.backmusic(res.tapIndex)
        }
        console.log(res.tapIndex)
      },
      fail (res) {
        console.log(res.errMsg)
      }
    })
  },
  backmusic: function (index) {
       let bgm = ''
       if(index === 1){
         bgm = 'forest'
       }
       if(index === 2){
        bgm = 'night'
       }
       if(index === 3){
        bgm = 'rain'
       }
       if(index === 4){
        bgm = 'waves'
       }
       player();
       function player() {
         back.title = "bgm";   // 必须要有一个title
         back.src = "http://127.0.0.1:8443/habit/api/file/bgm/" + bgm + ".mp3";//音频文件获取
         //back.src = "https://oahouhs.xyz/api/file/"+bgm+".mp3";
         back.onEnded(() => {
           player();  // 音乐循环播放
         })
      }
   },
})
